package edu.jxut.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.jxut.reggie.common.R;
import edu.jxut.reggie.dtos.DishDto;
import edu.jxut.reggie.pojos.Dish;
import edu.jxut.reggie.server.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;



    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("保存成功!");
    }

    @GetMapping("/page")
    public R<Page> queryByPage(@RequestParam("page")Integer page
            ,@RequestParam("pageSize")Integer pageSize
            ,@RequestParam(value = "name",required = false)String name){

        Page<DishDto> byPage = dishService.findByPage(page, pageSize, name);
        return R.success(byPage);
    }

    @GetMapping("{id}")
    public R<DishDto> queryOneById(@PathVariable("id") long id){
        DishDto byIdWithFlavor = dishService.getByIdWithFlavor(id);
        return R.success(byIdWithFlavor);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }

    @PostMapping("status/{status}")
    public R<String> batchDisable(@PathVariable("status")Integer status,@RequestParam("ids")String ids){
        //分割删除id
        String[] split = ids.split("\\,");
        for (String id : split) {
            long idL = Long.parseLong(id);
            Dish dish = new Dish();
            dish.setId(idL);
            dish.setStatus(status);
            dishService.updateById(dish);
        }
        return R.success("停售成功！");
    }


    @DeleteMapping
    public R<String> bathDelete(@RequestParam("ids")String ids){
        //分割删除id
        dishService.rmIds(ids);
        return R.success("删除成功");
    }

    @GetMapping("list")
    public R<List> getList(@RequestParam("categoryId")Long categoryId){
        List<DishDto> byCateIdWithFlavor = dishService.getByCateIdWithFlavor(categoryId);
        return R.success(byCateIdWithFlavor);
    }
}
